package unipd.esp1112.pro;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


/**
 * Activity where the user can edit or delete a profile
 * 
 * @author andrea basso, federico lancerin, alex tomasello
 * 
 */
public class Opzioni extends Activity {

	/**
	 * button that brings the user to edit the current profile
	 */
	private Button profileEdit;
	/**
	 * button that leads to the deletion of the current profile
	 */
	private Button profileDelete;
	/**
	 * variable used to retrieve an instance of the application private database
	 */
	private MyOpenHelper op;
	/**
	 * variable used to reference the database
	 */
	private SQLiteDatabase db;
	/**
	 * variable to store the name of the profile the user is currently using
	 */
	private String nomeProfilo;
	/**
	 * alertdialog that pops up when the user press "profileDelete" button to ask for a confirm before proceeding
	 */
	private AlertDialog.Builder builder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.optionmenu);

		Intent intent = getIntent();
		nomeProfilo = intent.getStringExtra("profilo");
		builder = new AlertDialog.Builder(this);

		op = new MyOpenHelper(this);
		profileEdit = (Button) findViewById(R.id.profileEdit);
		profileEdit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(getApplicationContext(), NuovoProfilo.class);
				myIntent.putExtra("profilo", nomeProfilo);
				startActivityForResult(myIntent, 0);
			}
		});

		profileDelete = (Button) findViewById(R.id.profileDelete);
		profileDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.setMessage("Sicuro di voler eliminare questo profilo e tutti i suoi dati?").setCancelable(false)
						.setPositiveButton("Si", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								db = op.getWritableDatabase();
								db.delete(op.TABLE2, "nome='" + nomeProfilo + "'", null);
								db.delete(op.TABLE1, "nome='" + nomeProfilo + "'", null);
								db.close();
								Intent intent = new Intent();
								setResult(RESULT_OK, intent);
								Menu.daTerminare = true;
								finish();
								Toast toast = Toast.makeText(getApplicationContext(), "Profilo \"" + nomeProfilo + "\" cancellato.", Toast.LENGTH_SHORT);
								toast.setGravity(Gravity.CENTER, 0, 0);
								toast.show();
							}
						}).setNegativeButton("No", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
				builder.create();
				builder.show();
			}
		});
	}
}
